defmodule ChoicePlayground.PageControllerTest do
  use ChoicePlayground.ConnCase

  @questions %{
    "id" => 1,
    "title" => "程序员信仰测试",
    "questions" => [
      %{
        "id" => 1,
        "type" => "select",
        "title" => "请选择你最喜欢的语言",
        "required" => true,
        "options" => [
          %{
            "id" => 1,
            "content" => "PHP"
          },
          %{
            "id" => 2,
            "content" => "Ruby"
          },
          %{
            "id" => 3,
            "content" => "Elixir"
          },
          %{
            "id" => 4,
            "content" => "JavaScript"
          }
        ]
      },
      %{
        "id" => 2,
        "type" => "fill",
        "title" => "请填写你喜欢它的原因",
        "required" => false,
      }
    ]
  }

  test "GET /v1/surveys/1" do
    response = build_conn()
    |> get(page_path(build_conn(), :index))
    |> json_response(200)

    expected = @questions

    assert response == expected
  end


  test "POST /v1/surveys/1/answer" do
    answerResult = fn(reason) -> %{
      "selection" => %{"title" => "你最喜欢的语言", "content" => "PHP Ruby Elixir" },
      "fill" => %{"title" => "你喜欢它的原因", "content" => reason}
    } end

    # no fill content
    response1 = build_conn()
    |> post(page_path(build_conn(), :answer), %{"select" => [1, 2, 3]})
    |> json_response(200)
    assert response1 == answerResult.("")

    # selection and fill content
    response2 = build_conn()
    |> post(page_path(build_conn(), :answer), %{"select" => [1, 2, 3], "fill" => "你猜"})
    |> json_response(200)
    assert response2 == answerResult.("你猜")

    # no any content
    response3 = build_conn()
    |> post(page_path(build_conn(), :answer))
    |> json_response(400)
    assert response3 == "该请求不可处理"

    # selection is wrong
    response4 = build_conn()
    |> post(page_path(build_conn(), :answer), %{"select" => [5, 5], "fill" => "你猜"})
    |> json_response(400)
    assert response4 == "语言必须选一项"
    response4 = build_conn()
    |> post(page_path(build_conn(), :answer), %{"select" => [5]})
    |> json_response(400)
    assert response4 == "语言必须选一项"
  end
end
